package com.xinke.module.system.controller.admin.social;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.social.*;
import com.xinke.module.system.model.entity.social.SocialUserDO;
import com.xinke.module.system.service.social.SocialUserService;

@Tag(name = "管理后台 - 社交用户")
@RestController
@RequestMapping("/system/social-user")
@Validated
public class SocialUserController {

    @Resource
    private SocialUserService socialUserService;

    @PostMapping("/create")
    @Operation(summary = "创建社交用户")
    @PreAuthorize("@ss.hasPermission('system:social-user:create')")
    public Result<Long> createSocialUser(@Valid @RequestBody SocialUserSaveReqVO createReqVO) {
        return success(socialUserService.createSocialUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社交用户")
    @PreAuthorize("@ss.hasPermission('system:social-user:update')")
    public Result<Boolean> updateSocialUser(@Valid @RequestBody SocialUserSaveReqVO updateReqVO) {
        socialUserService.updateSocialUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社交用户")
    @Parameter(name = "socialUserId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:social-user:delete')")
    public Result<Boolean> deleteSocialUser(@RequestParam("socialUserId") Long socialUserId) {
        socialUserService.deleteSocialUser(socialUserId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社交用户")
    @Parameter(name = "socialUserId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:social-user:query')")
    public Result<SocialUserRespVO> getSocialUser(@RequestParam("socialUserId") Long socialUserId) {
        SocialUserDO socialUser = socialUserService.getSocialUser(socialUserId);
        return success(BeanUtils.toBean(socialUser, SocialUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社交用户分页")
    @PreAuthorize("@ss.hasPermission('system:social-user:query')")
    public Result<PageResult<SocialUserRespVO>> getSocialUserPage(@Valid SocialUserPageReqVO pageReqVO) {
        PageResult<SocialUserDO> pageResult = socialUserService.getSocialUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SocialUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社交用户 Excel")
    @PreAuthorize("@ss.hasPermission('system:social-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSocialUserExcel(@Valid SocialUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SocialUserDO> list = socialUserService.getSocialUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社交用户.xls", "数据", SocialUserRespVO.class,
                        BeanUtils.toBean(list, SocialUserRespVO.class));
    }

}